package gui.common;

import gui.item.ItemData;
import gui.product.ProductData;
import core.ContainerType;
import core.CoreFacade;
import core.Item;
import core.Product;

/**
 * Controller is a base class that provides common functionality required
 * by all controllers in the program (i.e., all controller classes extend Controller).
 * <P>
 * The functionality provided by Controller to its subclasses includes:
 * <UL>
 * <LI>A reference to the controller's view object
 * <LI>An abstract method interface that must be supported by all subclasses
 * <LI>A template method that defines a standard algorithm for constructing 
 * subclass instances
 * </UL>
 * <P>
 * Subclasses should override the following methods:
 * <UL>
 * <LI>{@link #enableComponents() enableComponents}
 * <LI>{@link #loadValues() loadValues}
  * </UL>
 * <P>
 * Subclasses should also call {@link #construct() construct} from their
 * constructors.
 */public abstract class Controller implements IController {

	/**
	 * Reference to the view for this controller.
	 */
	protected IView _view;

	/**
	 * Constructor.
	 * 
	 * @param view The view for this controller
	 * 
	 * {@pre view != null}
	 * 
	 * {@post getView() == view}
	 */
	protected Controller(IView view) {
		this._view = view;
	}
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	protected IView getView() {
		return _view;
	}
	
	/**
	 * This is a template method that defines the construction process 
	 * for all subclasses.  Subclasses should call this method from their 
	 * constructors in order to properly initialize themselves.
	 * 
	 * {@pre None}
	 * 
	 * {@post The controller has loaded data into its view, and set the enable/disable
	 * state of its view's components}
	 */
	protected void construct() {

		enableComponents();
		loadValues();
	}
	
	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	protected void loadValues() {
		return;
	}
	
	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	protected void enableComponents() {
		return;
	}
	
	
	
	/*
	 * added by SPencer Hoffa
	 */
	
	/**
	 * This method will convert a Product to the internal data unit of
	 * ProductData type. 
	 * 
	 * @param p
	 * @return
	 */
	protected ProductData convertToProductData(Product p, int numberOfProducts)
	{
		ProductData newData = new ProductData();
		
		newData.setBarcode(p.getBarcodeString());//p.getBarCode().getBarcode());
		newData.setCount(/*"" +*/ Integer.toString(numberOfProducts));
		//"" + addedItems.get(p).size());
		newData.setDescription(p.getDescription());
		newData.setShelfLife(String.valueOf( p.getShelfLife() ) );
		newData.setSize(p.getAmount().toString());
		newData.setSupply(String.valueOf( p.getThreeMonthSupply() ) );
		newData.setTag(p);
		
		return newData;
	}
	
	protected ItemData convertToItemData(Item i)
	{
		ItemData newData = new ItemData();

		newData.setBarcode(i.getBarcodeString());//i.getBarcode().getBarcode());
		newData.setEntryDate(i.getEntryDate().getTime());
		
		if (i.getExpirationDate() != null)
		{
			newData.setExpirationDate(i.getExpirationDate().getTime());
		}
		else
		{
			newData.setExpirationDate(null);
		}
		
		//newData.setProductGroup(i.getProductContainer().getName());
		if (i.getProductContainer().getContainerType() == ContainerType.STORAGE_UNIT)
		{
			newData.setStorageUnit(i.getProductContainer().getName());
		}
		else
		{
			newData.setProductGroup(i.getProductContainer().getName());
			try 
			{
				newData.setStorageUnit(
						CoreFacade.getInstance().getParentStorageUnit(
															i.getProductContainer()).getName());
			} 
			catch (Exception e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		newData.setTag(i);
		
		return newData;
	}
	
	/*
	 *------------------------------------------------------------------- 
	 */
	
}

